package com.mzj.saas.mservice.property.repository;

import com.mzj.saas.mservice.property.entity.ComplainSuggest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;

/**
 * @author hikari
 * @since 2021-05-13
 */
@Repository
public interface ComplainSuggestRepository extends JpaRepository<ComplainSuggest, Long>, JpaSpecificationExecutor<ComplainSuggest> {
    Page<ComplainSuggest> findByUserId(String userId, Pageable pageable);

    Page<ComplainSuggest> findByUserIdAndType(String userId, Integer type, Pageable pageable);

    @Query(value = "SELECT * FROM saas_complain_suggest WHERE user_id = ?1 AND create_time BETWEEN ?2 AND ?3 AND type = ?4",
            countQuery = "SELECT COUNT(*) FROM saas_complain_suggest WHERE user_id = ?1 AND create_time BETWEEN ?2 AND ?3 AND type = ?4",
            nativeQuery = true)
    Page<ComplainSuggest> findBy(String userId, LocalDateTime start, LocalDateTime end, Integer type, Pageable pageable);

    @Query(value = "SELECT * FROM saas_complain_suggest WHERE user_id = ?1 AND create_time BETWEEN ?2 AND ?3",
            countQuery = "SELECT COUNT(*) FROM saas_complain_suggest WHERE user_id = ?1 AND create_time BETWEEN ?2 AND ?3",
            nativeQuery = true)
    Page<ComplainSuggest> findBy(String id, LocalDateTime start, LocalDateTime end, Pageable pageable);
}
